package com.healthcloud.service.impl;

import com.healthcloud.constants.UserConsts;
import com.healthcloud.dao.HospDrAssoMapper;
import com.healthcloud.entity.HospDrAsso;
import com.healthcloud.entity.SysRoleEntity;
import com.healthcloud.entity.SysUserEntity;
import com.healthcloud.service.SysRoleService;
import com.healthcloud.service.SysUserService;
import com.healthcloud.utils.Constant;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.healthcloud.dao.HlDoctorDao;
import com.healthcloud.entity.HlDoctorEntity;
import com.healthcloud.service.HlDoctorService;
import com.healthcloud.utils.PageParam;
import org.springframework.transaction.annotation.Transactional;


@Service("hlDoctorService")
@Transactional
public class HlDoctorServiceImpl implements HlDoctorService {
	@Autowired
	private HlDoctorDao hlDoctorDao;

	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private HospDrAssoMapper hospDrAssoMapper;

	@Autowired
	private SysRoleService sysRoleService;

	@Override
	public HlDoctorEntity queryObject(Long id){
		return hlDoctorDao.queryObject(id);
	}


	@Override
	public HlDoctorEntity queryByUserId(Long userId) {
		return hlDoctorDao.queryByUserId(userId);
	}

	@Override
	public List<HlDoctorEntity> queryList(HlDoctorEntity bean, PageParam pageParam){
		return hlDoctorDao.queryList(bean, pageParam);
	}

	@Override
	public int queryTotal(HlDoctorEntity bean){
		return hlDoctorDao.queryTotal(bean);
	}
	
	@Override
	public void save(HlDoctorEntity hlDoctor){
		hlDoctorDao.save(hlDoctor);
	}
	
	@Override
	public void update(HlDoctorEntity hlDoctor){
		hlDoctorDao.update(hlDoctor);
	}
	
	@Override
	public void delete(Long id){
		hlDoctorDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Long[] ids){
		hlDoctorDao.deleteBatch(ids);
	}

	@Override
	public void register(SysUserEntity user, HlDoctorEntity doctor, Map<Long, List<Long>> map) {
		Date now = new Date();
		//注册用户基本信息，默认为医生账号
		user.setStatus(UserConsts.STATE_NORMAL);
		SysRoleEntity roleEntity = new SysRoleEntity();
		roleEntity.setRoleId(UserConsts.ROLE_DOCKER);
		List<SysRoleEntity> sysRoleEntities = new ArrayList<>();
		sysRoleEntities.add(roleEntity);
		user.setRoleList(sysRoleEntities);
		if (StringUtils.isNotEmpty(doctor.getUserSource())){
			user.setSource(Integer.valueOf(doctor.getUserSource()));
		}
		sysUserService.save(user);
		//新增医生基本信息
		doctor.setUserId(user.getId());
		doctor.setMkDrCode("");
		doctor.setDrName(user.getRealName());
		doctor.setCreatedTime(now);
		doctor.setUpdatedTime(now);
		hlDoctorDao.save(doctor);
		//新增医生，医院，科室的关联信息
		for(Map.Entry<Long, List<Long>> entry:map.entrySet()){
			Long hospId = entry.getKey();//医院id
			List<Long> officeCodes = entry.getValue();//科室id
			for (Long officeCode : officeCodes) {
				HospDrAsso asso = new HospDrAsso();
				asso.setDrId(doctor.getId());
				asso.setHospId(hospId);
				asso.setOfficeCode(officeCode);
				asso.setCreatedTime(new Date());
				asso.setUpdatedTime(new Date());
				hospDrAssoMapper.save(asso);
			}
		}
	}

	@Override
	public List<HospDrAsso> getHospDrAsso(HospDrAsso entity) {
		return hospDrAssoMapper.findByEntityAll(entity);
	}

	@Override
	public void update(SysUserEntity user, HlDoctorEntity doctor, Map<Long, List<Long>> map) {

		Date now = new Date();
		//注册用户基本信息
        user.setSource(Integer.valueOf(doctor.getUserSource()));
		sysUserService.update(user);
		//新增医生基本信息
		doctor.setUserId(user.getId());
		doctor.setMkDrCode("");
		doctor.setDrName(user.getRealName());
		doctor.setUpdatedTime(now);
		if(doctor.getId()==null||doctor.getId().equals("")){
			hlDoctorDao.save(doctor);
		}else{
			hlDoctorDao.update(doctor);
		}
		//根据医生id删除医生，医院，科室的关联信息
		hospDrAssoMapper.deleteByDrId(doctor.getId());
		//新增医生，医院，科室的关联信息
		for(Map.Entry<Long, List<Long>> entry:map.entrySet()){
			Long hospId = entry.getKey();//医院id
			List<Long> officeCodes = entry.getValue();//科室id
			for (Long officeCode : officeCodes) {
				HospDrAsso asso = new HospDrAsso();
				asso.setDrId(doctor.getId());
				asso.setHospId(hospId);
				asso.setOfficeCode(officeCode);
				asso.setCreatedTime(now);
				asso.setUpdatedTime(now);
				hospDrAssoMapper.save(asso);
			}
		}

	}

}
